#include "read.h"

Read_Class::Read_Class ()
{
  for (int i = 0; i < 10; i++)
	vec[i] = NULL;
  vec_pos = 0;
}

boolean Read_Class::add_vec (Vector* data)
{
  if (10 == vec_pos)
  {
	cout << "Cannot add anymore arrays!" << endl;
	return FALSE;
  }
  else
  {
	for (int i = 0; i < vec_pos; i++)
	  if (vec[i]->get_name () == data->get_name ())
	  {
		cout << "Vector already added!" << endl;
		return FALSE;
	  }
	vec[vec_pos++] = data;
	return TRUE;
  }
}

int Read_Class::read_data (string filename)
{
  int num = 0, count, i;
  string items[100];

  if(!f_in.open (filename, 'r'))
  {
	cout << "Cannot open input file!" << endl;
	return -1;
  }
  else
  {
	while (!f_in.eof ())
	{
	  if (0 == (count = f_in.next (items, 100)))  // no data on a line
		return -1;
	  if (count < vec_pos)
	  { // there are less data items on a line than the number of vectors
		cout << "Insufficient amount of data!" << endl;
		return -1;
	  }
	  for (i = 0; i < vec_pos; i++)
	  {
		if (vec[i]->add_item (items[i]))
		  num++;
		else
		{
		  cout << "Invalid data" << endl;
		  return -1;
		}
	  }
	} // end while
  }
  f_in.close ();
  vec_pos = 0;      		 // reset the vec_pos to 0
  return num;
}